package org.edu.modules.jxgz.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.api.vo.Result;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.system.vo.LoginUser;
import org.edu.common.util.oConvertUtils;
import org.edu.modules.jxgz.api.CommonApi;
import org.edu.modules.jxgz.entity.VeJwGzzj;
import org.edu.modules.jxgz.entity.VeJwJxrz;
import org.edu.modules.jxgz.entity.VeJwText;
import org.edu.modules.jxgz.service.IVeJwJxrzService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.edu.modules.jxgz.util.VeJwTextUtils;
import org.edu.modules.scheduleCommon.entity.VeBaseAppUser;
import org.edu.modules.scheduleCommon.service.VeBaseAppUserService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.edu.common.aspect.annotation.AutoLog;

/**
 * @Description: ve_jw_jxrz
 * @Author: jeecg-boot
 * @Date: 2021-05-26
 * @Version: V1.0
 */
@Api(tags = "教学日志")
@RestController
@RequestMapping("/jxgz/veJwJxrz")
@Slf4j
public class VeJwJxrzController extends BaseController<VeJwJxrz, IVeJwJxrzService> {
    @Autowired
    private IVeJwJxrzService veJwJxrzService;
    @Autowired
    private CommonApi commonApi;


    @Autowired
    private VeBaseAppUserService veBaseAppUserService;

    /**
     * 常量
     */
    //未评审
    private static final Integer NOT_REVIEWED = 0;
    //已评审
    private static final Integer REVIEWED = 1;

    /**
     * 分页列表查询
     *
     * @param veJwJxrz
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "分页列表查询")
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(VeJwJxrz veJwJxrz,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (oConvertUtils.isEmpty(loginUser)) {
            return Result.error("请登录系统！");
        }
        QueryWrapper<VeJwJxrz> queryWrapper = QueryGenerator.initQueryWrapper(veJwJxrz, req.getParameterMap());

        if (veJwJxrz.getAdduser() == null) {
            //根据用户id查用户信息
            VeBaseAppUser appUser = veBaseAppUserService.getVeBaseAppUserById(loginUser.getId());
            if (appUser.getUserType().contains("1")) {
                //如果用户类型是1，为教师账号
                queryWrapper.eq("adduser", appUser.getUserId());
            }
        }

        Page<VeJwJxrz> page = new Page<VeJwJxrz>(pageNo, pageSize);
        IPage<VeJwJxrz> pageList = veJwJxrzService.page(page, queryWrapper);
        List<VeJwJxrz> records = pageList.getRecords();
        for (VeJwJxrz veJwJxrzTemp : records) {
            VeJwText veJwText = VeJwTextUtils.getVeJwTextById(veJwJxrzTemp.getRznrtid());
            if (veJwText != null) {
                veJwJxrzTemp.setRznrtidText(veJwText.getText());
            }
            veJwText = VeJwTextUtils.getVeJwTextById(veJwJxrzTemp.getVerifytid());
            if (veJwText != null) {
                veJwJxrzTemp.setVerifytidText(veJwText.getText());
            }
        }
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param veJwJxrz
     * @return
     */
    @AutoLog(value = "添加")
    @ApiOperation(value = "添加", notes = "添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody VeJwJxrz veJwJxrz) {
        String rznrtidText = veJwJxrz.getRznrtidText();
        if (StringUtils.isBlank(rznrtidText)) {
            return Result.error("内容不可为空");
        }
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (oConvertUtils.isEmpty(loginUser)) {
            return Result.error("请登录系统！");
        }
        //根据用户id查用户信息
        VeBaseAppUser appUser = veBaseAppUserService.getVeBaseAppUserById(loginUser.getId());
        veJwJxrz.setAddusername(appUser.getRealName());
        veJwJxrz.setVerifytype(NOT_REVIEWED);
        veJwJxrz.setRznrtid(VeJwTextUtils.add(rznrtidText));
        veJwJxrzService.save(veJwJxrz);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param veJwJxrz
     * @return
     */
    @AutoLog(value = "编辑")
    @ApiOperation(value = "编辑", notes = "编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody VeJwJxrz veJwJxrz) {
        String rznrtidText = veJwJxrz.getRznrtidText();
        if (StringUtils.isBlank(rznrtidText)) {
            return Result.error("内容不可为空");
        }
        VeJwTextUtils.update(veJwJxrz.getRznrtid(), rznrtidText);
        veJwJxrzService.updateById(veJwJxrz);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "通过id删除")
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        veJwJxrzService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "批量删除")
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.veJwJxrzService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "通过id查询")
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        VeJwJxrz veJwJxrz = veJwJxrzService.getById(id);
        if (veJwJxrz == null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(veJwJxrz);
    }


    /**
     * 评审
     *
     * @param veJwJxrz
     * @return
     */
    @AutoLog(value = "评审")
    @ApiOperation(value = "评审", notes = "评审")
    @PutMapping(value = "/audit")
    public Result<?> audit(@RequestBody VeJwJxrz veJwJxrz) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if (oConvertUtils.isEmpty(loginUser)) {
            return Result.error("请登录系统！");
        }
        //根据用户id查用户信息
        VeBaseAppUser appUser = veBaseAppUserService.getVeBaseAppUserById(loginUser.getId());
        veJwJxrz.setVerifyusername(appUser.getRealName());
        if (StringUtils.isNotBlank(veJwJxrz.getVerifytidText())) {
            veJwJxrz.setVerifytid(VeJwTextUtils.addOrUpdate(veJwJxrz.getVerifytid(), veJwJxrz.getVerifytidText()));
        }
        veJwJxrz.setVerifytime(new Date());
        veJwJxrz.setVerifytype(REVIEWED);
        veJwJxrzService.updateById(veJwJxrz);
        return Result.ok("编辑成功!");
    }


    /**
     * 导出excel
     *
     * @param request
     * @param veJwJxrz
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, VeJwJxrz veJwJxrz) {
        return super.exportXls(request, veJwJxrz, VeJwJxrz.class, "ve_jw_jxrz");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, VeJwJxrz.class);
    }

}
